﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Agents.Admin
{
    public partial class WaitingStockOrders : System.Web.UI.Page
    {
        /// <summary>
        /// Occurs when the page is loaded for the first time or being post back
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Handle item command
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void dgStockOrders_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            // Show specific stock order details
            if (e.CommandName == "ShowOrderDetails")
            {
                String redirectionString = "StockOrderDetails.aspx?param=" +
                    Request.QueryString["param"] + "&param2=" + Request.QueryString["param2"] +
                    "&stockOrderId=" + e.Item.Cells[0].Text;

                Response.Redirect(redirectionString);
            }
            else if (e.CommandName == "CancelOrder")
            {
                // Cancel the order in the DB
                Queries queries = new Queries();
                queries.CancelStockOrder(Int32.Parse(e.Item.Cells[0].Text));

                // Refresh the grid
                dgStockOrders.DataBind();
            }
            else if (e.CommandName == "OrderArrived")
            {
                // Update in the DB that the order arrived
                Queries queries = new Queries();
                queries.StockOrderArrived(Int32.Parse(e.Item.Cells[0].Text));

                // Refresh the grid
                dgStockOrders.DataBind();
            }
        }

        /// <summary>
        /// Handele the page indexing
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void dgStockOrders_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            // Updating the current page for the data grid to display.
            dgStockOrders.CurrentPageIndex = e.NewPageIndex;

            // Binding the data.
            dgStockOrders.DataBind();
        }

        /// <summary>
        /// Redirecting the admin back to his home page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void backBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/AdminMenu.aspx");
        }
    }
}